package com.whut.gkd.service.impl;

import com.whut.gkd.bean.ApplyGood;
import com.whut.gkd.bean.PageInfo;
import com.whut.gkd.dao.IApplyGoodDao;
import com.whut.gkd.service.IApplyGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplyGoodService implements IApplyGoodService {

    @Autowired
    private IApplyGoodDao applyGoodDao;

    @Override
    public PageInfo<ApplyGood> findAllApplyGood(int shopId, int currentPage, String goodName) {
        PageInfo pageInfo=new PageInfo();
        //每页默认显示的数量
        int size = 5;
        //开始初始化PageInfo对象
        int totalCount = applyGoodDao.getTotalCount(shopId,goodName);
        pageInfo.setTotalCount(totalCount);
        double d = totalCount/(size*1.0);
        int tp = (int)Math.ceil(d);
        pageInfo.setTotalPage(tp);
        pageInfo.setSize(size);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>tp){
            pageInfo.setCurrentPage(tp);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }
        int start = (pageInfo.getCurrentPage()-1)*size;
        //查找数据库，得到所有商品
        List<ApplyGood> applyGoodList = null;
        if(totalCount > 0) {
            applyGoodList = applyGoodDao.findAllApplyGood(shopId, start, size, goodName);
        }
        pageInfo.setList(applyGoodList);
        //返回PageInfo对象
        return pageInfo;
    }

    @Override
    public void addApplyGood(ApplyGood applyGood) {
        int count = applyGoodDao.getCountByApplyId(applyGood.getApplyId());
        if(count<=0) {
            applyGoodDao.addApplyGood(applyGood);
        }
    }

    @Override
    public void deleteApplyGoodByApplyId(int applyId) {
        int count = applyGoodDao.getCountByApplyId(applyId);
        if(count>0) {
            applyGoodDao.deleteApplyGoodByApplyId(applyId);
        }
    }

    @Override
    public ApplyGood findApplyGoodByApplyId(int applyId) {
        int count = applyGoodDao.getCountByApplyId(applyId);
        if(count>0) {
            return applyGoodDao.findApplyGoodByApplyId(applyId);
        }
        return null;
    }

    @Override
    public void modifyApplyGood(ApplyGood applyGood) {
        int count = applyGoodDao.getCountByApplyId(applyGood.getApplyId());
        if(count>0) {
            applyGoodDao.modifyApplyGood(applyGood);
        }
    }

    @Override
    public void deleteAllApplyGood(List<Integer> ids) {
        for(int i:ids){
            if(applyGoodDao.getCountByApplyId(i)>0) {
                applyGoodDao.deleteApplyGoodByApplyId(i);
            }
        }
    }

    @Override
    public PageInfo<ApplyGood> findAll(int currentPage,String shopName) {
        PageInfo<ApplyGood> pageInfo= new PageInfo();

        int totalCount=applyGoodDao.getTotalCounts(shopName);
        pageInfo.setTotalCount(totalCount);
        double d=totalCount/5.0;
        int tp= (int) Math.ceil(d);
        pageInfo.setTotalPage(tp);
        pageInfo.setSize(5);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>tp){
            pageInfo.setCurrentPage(tp);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }
        int start=(pageInfo.getCurrentPage()-1)*5;
        if(totalCount!=0){
            List<ApplyGood> goodsmanager= applyGoodDao.findAll(start,5,shopName);
            pageInfo.setList(goodsmanager);
        }else{
            pageInfo.setList(null);
        }

        return pageInfo;
    }

    @Override
    public void allowgoodByapplyId(int applyId) {
        ApplyGood applyGood = applyGoodDao.getApplyGoodById(applyId);

        applyGoodDao.allowgood(applyGood);
    }


    @Override
    public void deleteByapplyId(int applyId) {

        applyGoodDao.deleteByapplyId(applyId);
    }
}
